package com.briup.day26;

import com.sun.corba.se.spi.activation.Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Test_manyServeAppliance {
    public static void main(String[] args) throws Exception {
//        创建服务器对象
        ServerSocket server = new ServerSocket(8890);
        System.out.println("多线程服务器创建成功");
        while(true){
            //2.获取套接字(接受客户端对象)
            Socket socket = server.accept();
//            class MyHandler extends Thread{
//
//            }
            Thread th = new Thread(){
                public void run(){
                    BufferedReader br= null;
                    try{
                        //3.创建io流
                        InputStream is = socket.getInputStream();
                        br = new BufferedReader(new InputStreamReader(is));
                        String line = null;
                        //逐行读取客户端发送过来的数据
                        while ((line = br.readLine()) != null){
                            System.out.println(line);

                            if ("quit".equals(line)){
                                break;
                            }
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                    }finally {
                        try {
                            br.close();
                        }catch(Exception e){
                            throw  new RuntimeException(e);
                        }
                        try {
                            socket.close();
                        }catch(Exception e){
                            throw new RuntimeException(e);
                        }
                        try{
                            server.close();
                        }catch(Exception e){
                           throw new RuntimeException(e);
                        }
                    }
                }
            };
            th.start();

        }
    }
}
